/**
 * Created by Administrator on 2017/10/12.
 */

/*
var toolbar = new joint.ui.Toolbar({
    tools: [
        {type:'label',text:'标准库数据模型'},
        { type: 'separator' },
        { type: 'button', name: 'max', text: '放大' },
        { type: 'button', name: 'min', text: '减小' },
        { type: 'separator' },
        { type: 'button', name: 'undo', text: '撤销' },
        { type: 'button', name: 'redo', text: '重做' },
        { type: 'separator' },
        { type: 'button', name: 'print', text: '打印' },
    ],
});

$('#app').prepend(toolbar.render().el);




toolbar.on('max:pointerclick', function(event) {
    console.log('cancel clicked');
    paperScroller.zoom(0.2, { max: 4 });
});

toolbar.on('min:pointerclick', function(event) {
    console.log('cancel clicked');
    paperScroller.zoom(-0.2, { min: 0.2 })
});

var commandManager = new joint.dia.CommandManager({ graph: graph });
toolbar.on('undo:pointerclick', function(event) {
    commandManager.undo();
});

toolbar.on('redo:pointerclick', function(event) {
    commandManager.redo();
});

toolbar.on('print:pointerclick', function(event) {
    paper.print()
});
*/

$('.max').click(function(){
    paperScroller.zoom(0.2, { max:2});
});
$('.min').click(function(){
    paperScroller.zoom(-0.2, { min: 0.2 })
});
paperScroller.zoom();
var commandManager = new joint.dia.CommandManager({ graph: graph });
$('.undo').click(function(){
    commandManager.undo();
});
$('.redo').click(function(){
    commandManager.redo();
});

$('.print').click(function(){
    paper.print()
});

$('.svg').click(function(){
   paper.openAsSVG();
});

$('.png').click(function(){
   /* var mySvg = document.querySelector("#v-2");
    svgCon = mySvg.outerHTML;

    //svg转base64;
    href = 'data:image/svg+xml;base64,' + window.btoa(unescape(encodeURIComponent(svgCon)));
    image = new Image();
    image.src = href;
    image.onload=function() {
        var canvas = document.createElement('canvas');
        canvas.width = image.width;
        canvas.height = image.height;
        var context = canvas.getContext('2d');
        context.drawImage(image, 0, 0);

        //svg转png的base64编码;
        var imgDataUri = canvas.toDataURL('image/png');
        console.log(imgDataUri);
        //添加svg转base64格式的png图片
        var pngImg = document.createElement('img');
        pngImg.src = imgDataUri;
        //document.body.appendChild(pngImg)

        //下载png图片
        var a = document.createElement('a');
        a.download = "image.png";
        a.href = imgDataUri;
        a.click();
        //a.innerHTML = '下载png图片'
        //document.body.appendChild(a);
    }*/
    paper.openAsPNG();
});

var m1 = new MyModal.modal(function() {
    m1.hide();
});


$(".E_json").click(function(){
    var json = graph.toJSON();
    var j = JSON.stringify(json);
    $(".m-middle p").text(j);
    m1.show();
    $('.m-btn-cancel').css("display","none");
    $('.m-btn-sure').css("display","block");
    $('.m-btn-sure').click(function(){
        $(".m-middle p").text('');


    });
});
$('.I_json').click(function(){
    m1.show();
    $('.m-btn-cancel').css("display","block")
    $('.m-btn-sure').css("display","none");
    $('.m-btn-cancel').click(function(){
        var k =  $(".m-middle p").text();
       graph.fromJSON(JSON.parse(k));
        $(".m-middle p").text('');
    });
});

$('.layout').click(function(){
    var graphLayout = new joint.layout.TreeLayout({
        graph: graph,
        parentGap: 50,
        siblingGap: 50
    });
   /* var treeLayoutView = new joint.ui.TreeLayoutView({
        paper: paper,
        model: graphLayout
    });*/
    graphLayout.layout();
});

$('.search_t input').on('keypress',function(event){
    if(event.keyCode == 13)
    {
        paper.findViewByModel(rect)
    }
});









